#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
using namespace std;

//template<typename T>
bool cmp(vector<int> &a, vector<int> &b)
{
	return a[1] > b[1];
}

int main()
{
	int n;
	cin >> n;
	vector<vector<vector<int>>> gh(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int w;
			cin >> w;
			gh[i].push_back({ j, w });
		}
	}

	vector<int> vis(n, 0);
	vector<int> dis(n, INT32_MAX);
	priority_queue<vector<int>, vector<vector<int>>, decltype(*cmp)> pq(cmp);
	pq.push({ 0, 0 });

	int ans = 0;
	while (!pq.empty()) {
		auto e = pq.top();
		pq.pop();
		int u = e[0], w = e[1];

		if (!vis[u]) {
			vis[u] = 1;
			ans += w;

			for (auto e : gh[u]) {
				int v = e[0];
				if (!vis[v] && e[1] < dis[v]) {
					dis[v] = e[1];
					pq.push({ v, e[1] });
				}
			}
		}
	}

	cout << ans << endl;
	return 0;
}